package com.feicheng.controller.main.admin.client;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.VoucherRecord;
import com.feicheng.service.IFClientService;
import com.feicheng.service.IFPOIService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cailongyang
 * @since 2021-12-02
 */
@RestController("/main/admin/client/voucher-record")
@RequestMapping("/main/admin/client/voucher-record")
public class VoucherRecordController {

	@Autowired
	IFClientService ifClientService;

	@Autowired
	IFPOIService fPOIService;

	@GetMapping("/off-line")
	public void exportVR(String name, String uuId, Boolean statu, String recordIdStr, HttpServletResponse response) {
		JSONArray recordIds = null;

		if (StringUtils.hasLength(recordIdStr)) {
			recordIds = JSONObject.parseArray(recordIdStr);
		}

		fPOIService.downLoadExcel(response, name, uuId, statu, recordIds);
	}

	/**
	 * 新增会员券
	 * 
	 * @param voucherRecord
	 * @return
	 */
	@PutMapping("/info")
	public ResultInfo updateVoucher(String voucherRecord) {
		return ifClientService.updateVoucher(JSONObject.parseObject(voucherRecord, VoucherRecord.class));
	}

	/**
	 * 通过id查会员券信息
	 * 
	 * @param voucherId
	 * @return
	 */
	@GetMapping("/info")
	public ResultInfo getVoucher(Integer voucherId) {
		return ifClientService.getVoucher(voucherId);
	}

	/**
	 * 批量新增会员券信息
	 * 
	 * @param voucherTypeId
	 * @param count
	 * @param statu
	 * @return
	 */
	@PostMapping("/info")
	public ResultInfo newVouchers(Integer voucherTypeId, Integer count, Boolean statu) {

		return ifClientService.newVouchers(voucherTypeId, count, statu);
	}

	/**
	 * 分页查询会员券信息
	 * 
	 * @param name
	 * @param uuId
	 * @param statu
	 * @param current
	 * @param size
	 * @return
	 */
	@GetMapping("/page")
	public ResultInfo getVouchersByPage(String name, String uuId, Boolean statu,
			@RequestParam(required = false, defaultValue = "1") Integer current,
			@RequestParam(required = false, defaultValue = "10") Integer size) {
		JSONObject param = new JSONObject();
		param.put(Constant.ENTITY.VOUCHER_TYPE.NAME, name);
		param.put(Constant.ENTITY.VOUCHER_RECORD.UU_ID, uuId);
		param.put(Constant.ENTITY.VOUCHER_RECORD.STATU, statu);
		param.put(Constant.HTTP.PARAM.CURRENT, current);
		param.put(Constant.HTTP.PARAM.SIZE, size);
		return ifClientService.getVouchersByPage(param);
	}

}
